<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="service.GoodsService" %>
<%@ page import="model.Goods" %>
<%@ page import="java.util.List" %>
<%@ page import="service.UserService" %>
<%@ page import="model.User" %>
<%
    UserService userService = new UserService();
    List<User> userList = userService.getUserList();
    request.setAttribute("userList", userList);

    //    获取商品id
    GoodsService goodsService = new GoodsService();
    List<Goods> goodsList = goodsService.getGoodsList();
    request.setAttribute("goodsList", goodsList);
    String goodsId = request.getParameter("id");
    if (goodsId != null) {
        Goods goods = goodsService.getGoodsById(Integer.parseInt(goodsId));
        request.setAttribute("g", goods);
    }

%>


<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>商城管理系统</title>
    <link rel="stylesheet" href="css/userlist.css">
    <link rel="stylesheet" href="css/addgoods.css">
    <script SRC="js/jquery.min.js"></script>
</head>

<body>
<div class="container">
    <!-- 左侧竖状分类栏 -->
    <div class="sidebar">
        <div class="logo">
            <img src="image/mi-icon.png " style="width: 50px; height: 50px">
        </div>

        <ul>
            <li><a href="#" data-target="userList">用户管理</a></li>
            <li><a href="#" data-target="addUser">用户添加</a></li>
            <li><a href="#" data-target="permissionManage">商品管理</a></li>
            <li><a href="#" data-target="infoModify">添加商品</a></li>
            <li><a href="#" data-target="infoModify" onclick="confirmUser()">返回首页</a></li>
        </ul>
    </div>
    <!-- 右侧内容展示区域 -->
    <div class="main-content">
        <div id="userList" class="content-section">
            <h2>用户列表</h2>
            <table id="dataTable">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>账号</th>
                    <th>密码</th>
                    <th>邮箱</th>
                    <th>电话</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${userList}" var="u">
                    <tr>
                        <td>${u.id}</td>
                        <td>${u.account}</td>
                        <td>${u.password}</td>
                        <td>${u.email}</td>
                        <td>${u.phone}</td>
                        <td class="action-buttons">
                            <button class="btn btn-danger" onclick="deleteUser(${u.id})">删除</button>
                            <button class="btn btn-warning"><a href="AdminUserEditshowServlet?id=${u.id}" STYLE="text-decoration: none;color: #333333"> 修改</a>
                            </button>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
        <div id="addUser" class="content-section hide">
            <h2>添加用户</h2>
            <c:if test="${!empty msg}">
                <div class="alert alert-danger">${msg}</div>
            </c:if>
            <form id="addUserForm" action="UserAdminAdd" method="post">
                <label for="newAccount">账号：</label>
                <input type="text" id="newAccount" required name="account"><br>
                <label for="newPassword" name="">密码：</label>
                <input type="password" id="newPassword" required name="password"><br>
                <label for="newEmail">邮箱：</label>
                <input type="email" id="newEmail" required name="email"><br>
                <label for="newPhone">电话：</label>
                <input type="text" id="newPhone" required name="phone"><br>
                <input type="submit" value="添加用户" class="btn btn-primary">
            </form>
        </div>

        <div id="permissionManage" class="content-section hide">
            <h2>商品管理</h2>
            <ul class="permission-list">
                <table id="dataTable1">
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>商品名字</th>
                        <th>商品样式</th>
                        <th>商品价格</th>
                        <th>商品介绍</th>
                        <th>商品库存</th>
                        <th>商品类型</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${goodsList}" var="g">
                        <tr>
                            <td>${g.id}</td>
                            <td>${g.name}</td>
                            <td><img src=".${g.cover}" alt="${g.name}" style="width: 60px;height: 60px;" alt=""/></td>
                            <td>${g.price}</td>
                            <td>${g.intro}</td>
                            <td>${g.stock}</td>
                            <td>${g.type.id}</td>
                            <td class="action-buttons">
                                <button class="btn btn-danger" onclick="deletegoods(${g.id})">删除</button>
                                <button class="btn btn-warning"><a href="AdminGoodsEditshowServlet?id=${g.id}" STYLE="text-decoration: none;color: #333333"> 修改</a></button>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </ul>
        </div>
        <div id="infoModify" class="content-section hide">
            <div class="form-container">
                <h2 class="form-title">添加商品信息</h2>
                <form class="form-horizontal" action="goods_add" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="input_name" class="control-label">商品名称</label>
                        <input type="text" class="form-control" id="input_name" name="name" required="required"
                               placeholder="请输入商品名称">
                    </div>

                    <div class="form-group">
                        <label for="input_price" class="control-label">商品价格</label>
                        <input type="number" class="form-control" id="input_price" name="price" required="required"
                               placeholder="请输入商品价格" min="0">
                    </div>

                    <div class="form-group">
                        <label for="input_intro" class="control-label">商品介绍</label>
                        <textarea class="form-control" id="input_intro" name="intro" rows="3"
                                  placeholder="请输入商品介绍"></textarea>
                    </div>

                    <div class="form-group">
                        <label for="input_stock" class="control-label">库存数量</label>
                        <input type="number" class="form-control" id="input_stock" name="stock" required="required"
                               placeholder="请输入库存数量" min="0">
                    </div>

                    <div class="form-group">
                        <label for="input_file" class="control-label">商品图片</label>
                        <div class="file-input-wrapper">
                            <div class="file-input-trigger">选择图片</div>
                            <input type="file" name="cover" id="input_file" required="required">
                        </div>
                        <span class="file-input-text">推荐尺寸：500*500</span>
                    </div>

                    <div class="form-group">
                        <label for="select_topic" class="control-label">商品类别</label>
                        <select class="form-control" id="select_topic" name="typeid">
                            <option value="1">手机</option>
                            <option value="2">平板</option>
                            <option value="3">电脑</option>
                            <option value="4">汽车</option>
                            <option value="5">家电</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="submit-btn">添加商品</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        // 默认显示用户列表板块
        $('#userList').show();

        // 给左侧菜单链接添加点击事件
        $('.sidebar ul li a').click(function (e) {
            e.preventDefault();
            var target = $(this).data('target');

            // 隐藏所有内容板块
            $('.content-section').hide();

            // 显示点击对应的板块
            $('#' + target).show();
        });
    });

    // 示例的删除用户函数（实际需结合后端实现真正删除功能）
    function deletegoods(goodsId) {
        if (confirm("确定要删除该商品吗？")) {
            // 这里可以添加Ajax请求等代码来调用后端删除接口
            $.ajax({
                url: "goods_delete?id=" + goodsId,
                type: "POST",
                success: function (response) {
                    console.log(response);
                    // 刷新页面
                    alert("删除成功！")
                    location.reload();
                },
                error: function (xhr, status, error) {
                    console.error("删除商品时出错: " + error);
                }
            });
            console.log("准备删除商品，用户ID：" + goodsId);
        }
    }

    function deleteUser(goodsId) {
        if (confirm("确定要删除该用户吗？")) {
            // 这里可以添加Ajax请求等代码来调用后端删除接口
            $.ajax({
                url: "user_delete?id=" + goodsId,
                type: "GET",
                success: function (response) {
                    console.log(response);
                    // 刷新页面
                    alert("删除成功！")
                    location.reload();
                },
                error: function (xhr, status, error) {
                    console.error("删除商品时出错: " + error);
                }
            });
            console.log("准备删除商品，用户ID：" + goodsId);
        }
    }

    // 示例的编辑用户函数（实际需结合后端实现真正编辑功能）
    function editUser(userId) {
        if (confirm("确定要修改该用户吗？")) {
            // 这里可以添加Ajax请求等代码来调用后端删除接口
            $.ajax({
                url: "AdminUserEditshowServlet?id=" + userId,
                type: "GET",
            })
        }
    }

    function confirmUser() {
        if (confirm("您确定要返回首页吗？")) {
            window.location.href = "index.jsp"
        }
    }
</script>
</body>

</html>